<template>
  <view class="demo-container">
    <!-- 基础用法 -->
    <view class="demo-block">
      <view class="demo-block__title">基础用法</view>
      <view class="demo-block__content">
        <fu-waterfall ref="waterfall">
          <template #left>
            <fu-waterfall-item
              v-for="item in leftItems"
              :key="item.id"
              :image="item.image"
              :title="item.title"
              :desc="item.desc"
              column="left"
              @height-change="onHeightChange"
              @click="onClick(item)"
            />
          </template>
          
          <template #right>
            <fu-waterfall-item
              v-for="item in rightItems"
              :key="item.id"
              :image="item.image"
              :title="item.title"
              :desc="item.desc"
              column="right"
              @height-change="onHeightChange"
              @click="onClick(item)"
            />
          </template>
        </fu-waterfall>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      leftItems: [],
      rightItems: [],
      items: [
        {
          id: 1,
          image: '/static/images/1.jpg',
          title: '标题1',
          desc: '描述文字1'
        },
        {
          id: 2,
          image: '/static/images/2.jpg',
          title: '标题2',
          desc: '描述文字2'
        },
        {
          id: 3,
          image: '/static/images/3.jpg',
          title: '标题3',
          desc: '描述文字3'
        },
        {
          id: 4,
          image: '/static/images/4.jpg',
          title: '标题4',
          desc: '描述文字4'
        }
      ]
    }
  },
  created() {
    // 初始化数据到瀑布流
    this.items.forEach(item => {
      const column = this.$refs.waterfall?.getShorterColumn() || 'left'
      if (column === 'left') {
        this.leftItems.push(item)
      } else {
        this.rightItems.push(item)
      }
    })
  },
  methods: {
    onClick(item) {
      uni.showToast({
        title: `点击了${item.title}`,
        icon: 'none'
      })
    },
    onHeightChange({ column, height }) {
      this.$refs.waterfall.updateColumnHeight(column, height)
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-container {
  padding: 20rpx;
}

.demo-block {
  margin-bottom: 30rpx;
  
  &__title {
    margin-bottom: 20rpx;
    font-size: var(--fu-font-size-lg);
    color: var(--fu-text-color);
  }
  
  &__content {
    border-radius: var(--fu-radius-base);
    overflow: hidden;
  }
}
</style> 